import request from '@/utils/request'
import { randomId, json2FormData } from '@/utils'
import memCache from '@/utils/memCache'

// 根据id获取详情
export function getDetailById(designId) {
  return request({
    method: 'GET',
    url: `/app/v1/scheme/2.0/detail/${designId}`
  })
}

// 分页获取-全屋空间：ADS方案管理中正常状态方案类型为“普通方案、主推样板间、新品方案、套餐方案、样板间”
export function getRoomPage(data) {
  // 方案类型:New->新品方案, ,Main->主推样板间,Package->套餐方案,Model_Room->样板间，Ordinary->普通方案,Building->楼盘方案，Product->单空间案例
  if (!data.programmeTypeCode || data.programmeTypeCode.length === 0) {
    const programmeTypeCode = ['New', 'Main', 'Package', 'Model_Room', 'Ordinary', 'Building']
    data.programmeTypeCode = programmeTypeCode
  }
  return request({
    method: 'POST',
    url: `/app/v1/scheme/2.0/page`,
    data,
    loading: false
  })
}

// 获取搜索条件-全屋空间：空间、风格、家庭类型、居室
export function getRoomScreenFun() {
  return request({
    method: 'GET',
    url: `/app/v1/scheme/2.0/attribute`
  }).then(res => {
    const result = []
    // 加入方案类型
    res.data.programmeTypeDTOS = [
      { typeName: '新品方案', value: 'New', selected: 1 },
      { typeName: '主推样板间', value: 'Main' },
      { typeName: '套餐方案', value: ',Package' },
      { typeName: '样板间', value: 'Model_Room' },
      { typeName: '普通方案', value: 'Ordinary' },
      { typeName: '楼盘方案', value: 'Building' }
    ]

    // 空间 adsSpaceDTOS
    result.push({
      title: '空间',
      flag: 'adsSpaceDTOS',
      unId: randomId(),
      show: true,
      children: (res.data.adsSpaceDTOS || []).map((e, i) => {
        e.label = e.spaceName
        // e.value = e.idx
        e.index = i
        return e
      })
    })
    // 风格 adsStyleDTOS
    result.push({
      title: '风格',
      flag: 'adsStyleDTOS',
      unId: randomId(),
      show: true,
      children: (res.data.adsStyleDTOS || []).map((e, i) => {
        e.label = e.styleName
        // e.value = e.idx
        e.index = i
        return e
      })
    })

    // 方案类型 programmeTypeDTOS
    result.push({
      title: '类型',
      flag: 'programmeTypeDTOS',
      unId: randomId(),
      show: true,
      multiple: true,
      children: (res.data.programmeTypeDTOS || []).map((e, i) => {
        e.label = e.typeName
        // e.value = e.idx
        e.index = i
        return e
      })
    })

    // 家庭类型 familyNumbersDTOS
    result.push({
      title: '家庭类型',
      flag: 'familyNumbersDTOS',
      unId: randomId(),
      show: false,
      children: (res.data.familyNumbersDTOS || []).map((e, i) => {
        e.label = e.familyNumberName
        // e.value = e.idx
        e.index = i
        return e
      })
    })
    // 户型(居室) livingRoomsDTOS
    result.push({
      title: '户型',
      flag: 'livingRoomsDTOS',
      unId: randomId(),
      show: false,
      children: (res.data.livingRoomsDTOS || []).map((e, i) => {
        e.label = e.livingRoomName
        // e.value = e.idx
        e.index = i
        return e
      })
    })
    return {
      rawDdata: res.data,
      data: result
    }
  })
}
export const getRoomScreen = memCache('getRoomScreen2.0', getRoomScreenFun)

// 获取搜索条件-单空间：空间、风格
export function getSingeAttributeFun() {
  return request({
    method: 'GET',
    url: `/app/v1/scheme/2.0/attribute`
  }).then(res => {
    // 处理数据结构，符合NavFilter组件
    const result = []
    // 空间 adsSpaceDTOS
    result.push({
      title: '空间',
      flag: 'adsSpaceDTOS',
      unId: randomId(),
      children: (res.data.adsSpaceDTOS || []).map((e, i) => {
        e.label = e.spaceName
        e.value = e.idx
        e.index = i
        return e
      })
    })
    // 风格 adsSpaceDTOS
    result.push({
      title: '风格',
      flag: 'adsStyleDTOS',
      unId: randomId(),
      children: (res.data.adsStyleDTOS || []).map((e, i) => {
        e.label = e.styleName
        e.value = e.idx
        e.index = i
        return e
      })
    })
    return {
      rawDdata: res.data,
      data: result
    }
  })
}
export const getSingeAttribute = memCache('getSingeAttribute2.0', getSingeAttributeFun)

// 分页获取-单空间
export function getSingePage(data) {
  data.programmeTypeCode = ['Product']
  return request({
    method: 'POST',
    url: `/app/v1/scheme/2.0/page`,
    data,
    loading: false
  })
}

// /scheme/getSchemeSearchPage
// 分页获取-方案搜索
export function getSchemeSearchPage(data) {
  return request({
    method: 'POST',
    url: `/app/v1/scheme/2.0/search`,
    data: json2FormData(data),
    loading: false
  })
}

// 相似方案
export function getProgramDataToList(id) {
  return request({
    method: 'GET',
    url: `/app/v1/scheme/2.0/recommend/${id}`,
    loading: false
  })
}
